package com.zsy.petshop_01.service.impl;

import com.zsy.petshop_01.common.result.ResponseResult;
import com.zsy.petshop_01.manager.SubscribeManager;
import com.zsy.petshop_01.model.dto.ShowPetCheckNum;
import com.zsy.petshop_01.model.entity.PetBaseInfo;
import com.zsy.petshop_01.model.entity.PetCheckUser;
import com.zsy.petshop_01.model.entity.PetType;
import com.zsy.petshop_01.model.vo.Pet.PetUpdateVo;
import com.zsy.petshop_01.model.vo.Pet.PetVo;
import com.zsy.petshop_01.model.vo.Pet.SubscribeCheckVo;
import com.zsy.petshop_01.model.vo.Pet.UpvoteVo;
import com.zsy.petshop_01.manager.PetManager;
import com.zsy.petshop_01.service.PetService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;

@Service
public class PetServiceImpl implements PetService {
    @Autowired
    PetManager petManager;
    @Autowired
    MinIOService minIOService;
    @Autowired
    SubscribeManager subscribeManager;

    @Override
    public ResponseResult<?> selectAllPet() {
        List<PetBaseInfo> petBaseInfos = petManager.selctAll();
        List<PetBaseInfo> petsResult = petManager.changeType(petBaseInfos);
        return ResponseResult.success("Ok", petsResult);
    }

    @Override
    public ResponseResult<?> deletePet(Integer id) {
        petManager.deletePet(id);
        return ResponseResult.success("OK");
    }

    @Override
    public ResponseResult<?> insetPet(PetVo petVo) {
        PetBaseInfo petBaseInfo = new PetBaseInfo();
        BeanUtils.copyProperties(petVo, petBaseInfo);
        MultipartFile file = petVo.getPetPicture();
        String filePath = "";
        try {
            filePath = minIOService.uploadFile(file);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        petBaseInfo.setPetPicture(filePath);
        petBaseInfo.setPetUpvote(0);
        petManager.insetPet(petBaseInfo);
        return ResponseResult.success("OK");
    }

    @Override
    public ResponseResult<?> updatePet(PetUpdateVo petUpdateVo) {
        PetBaseInfo petBaseInfo = new PetBaseInfo();
        BeanUtils.copyProperties(petUpdateVo, petBaseInfo);
        MultipartFile file = petUpdateVo.getPetPicture();
        if (file != null) {
            String filePath = "";
            try {
                filePath = minIOService.uploadFile(file);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            petBaseInfo.setPetPicture(filePath);
        }
        petManager.updatePet(petBaseInfo);
        return ResponseResult.success("OK");
    }

    @Override
    public ResponseResult<?> selectPet(Integer age, String size, String type, Map<String, Integer> priceSection) {
        List<PetBaseInfo> petBaseInfos = petManager.selectPet(age, size, type, priceSection);
        List<PetBaseInfo> petsResult = petManager.changeType(petBaseInfos);
        return ResponseResult.success("OK", petsResult);
    }

    @Override
    public ResponseResult<?> upvote(UpvoteVo upvoteVo) {
        Boolean isUpvote = upvoteVo.getIsUpvote();
        Integer id = upvoteVo.getId();
        if (isUpvote) {
            petManager.upvote(id);
        } else {
            petManager.downVote(id);
        }
        return ResponseResult.success("OK");
    }

    @Override
    public ResponseResult<?> selectPetType() {
        List<PetType> petTypes = petManager.selectPetType();
        return ResponseResult.success("OK", petTypes);
    }

    @Override
    public ResponseResult<?> insertPetType(Integer petNum, String petType) {
        PetType petType1 = new PetType();
        petType1.setPetNum(petNum);
        petType1.setPetType(petType);
        petManager.insertPetType(petType1);
        return ResponseResult.success("OK");
    }

    @Override
    public ResponseResult<?> updatePetType(Integer id, String petType) {
        petManager.updatePetType(id, petType);
        return ResponseResult.success("OK");
    }

    @Override
    public ResponseResult<?> deletePetType(Integer id) {
        petManager.deletePetType(id);
        return ResponseResult.success("OK");
    }

    @Override
    public ResponseResult<?> selectPetCheck() {
        List<ShowPetCheckNum> showPetCheckNums = petManager.selectPetCheck();
        return ResponseResult.success(showPetCheckNums);
    }

    @Override
    public ResponseResult<?> subscribeCheck(SubscribeCheckVo subscribeCheckVo, HttpServletRequest request) {
        Integer userId = Integer.parseInt(request.getHeader("user_id"));
        List<Integer> checkIds = subscribeCheckVo.getCheckIds();
        Integer petNum = subscribeCheckVo.getPetNum();
        String result = petManager.subscribeCheck(checkIds, petNum, userId);
        if(result.equals("Success"))
            return ResponseResult.success();
        else
            return ResponseResult.fail(result);
    }

    @Override
    public ResponseResult<?> cancelSubscribeCheck(Integer id) {
        petManager.cancelSubscribeCheck(id);
        return ResponseResult.success("OK");
    }

    @Override
    public ResponseResult<?> selectAllCheck() {
        List<PetCheckUser> petCheckUsers = petManager.selectAllCheck();
        return ResponseResult.success(petCheckUsers);
    }

    @Override
    public ResponseResult<?> selectCheckByUser(HttpServletRequest request) {
        Integer userId = Integer.parseInt(request.getHeader("user_id"));
        List<PetCheckUser> petCheckUsers = petManager.selectCheckByUser(userId);
        return ResponseResult.success(petCheckUsers);
    }

    @Override
    public ResponseResult<?> updateCheckMaxNum(Integer id, Integer maxNum) {
        petManager.updateCheckMaxNum(id, maxNum);
        return ResponseResult.success();
    }

    @Override
    public void initiativeUpdateTable() {
        petManager.createCheckSevenDays(LocalDate.now());
        subscribeManager.createSenvenDays(LocalDate.now());
    }
}
